package com.fwtai.tool;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

public final class ToolDate{

  /** 比较日期,大于true,否则false */
  public static boolean isBeforeDate(final LocalDate start,final LocalDate end){
    return end.isBefore(start);
  }

  /** 比较时间,大于true,否则false */
  public static boolean isBeforeTime(final LocalDateTime start,final LocalDateTime end){
    return end.isBefore(start);
  }

  /** 比较日期,大于true,否则false */
  public static boolean isBeforeDate(final String start,final String end){
    return isBeforeDate(toDate(start),toDate(end));
  }

  /** 比较时间,大于true,否则false */
  public static boolean isBeforeTime(final String start,final String end){
    return isBeforeTime(toTime(start),toTime(end));
  }

  /**是否相等 */
  public static boolean isEqualDate(final LocalDate start,final LocalDate end){
    return start.isEqual(end);
  }

  /**是否相等*/
  public static boolean isEqualTime(final LocalDateTime start,final LocalDateTime end){
    return start.isEqual(end);
  }

  public static LocalDate toDate(final CharSequence text){
    return LocalDate.parse(text);
  }

  public static LocalDateTime toTime(final CharSequence text){
    return LocalDateTime.parse(text);
  }

  //获取给定日期往后的连续12月的每月天数
  public static ArrayList<HashMap<String,String>> getMonths(final int coupon){
    final SimpleDateFormat formatY = new SimpleDateFormat("yyyy");
    final SimpleDateFormat formatM = new SimpleDateFormat("M");
    final ArrayList<HashMap<String,String>> lists = new ArrayList<>(12);
    final Calendar calendar = Calendar.getInstance();
    for(int i = 0; i < coupon; i++){
      calendar.setTime(new Date());
      calendar.add(Calendar.YEAR,0);
      calendar.add(Calendar.MONTH,i);
      final Date date = calendar.getTime();
      final String year = formatY.format(date);
      final String month = formatM.format(date);
      final HashMap<String,String> months = new HashMap<>(12);
      final String day = String.valueOf(getMoneyDay2(Integer.parseInt(year),Integer.parseInt(month)));
      final String end = year+"-"+month+"-"+day+" "+"23:59:59";
      final String start = year+"-"+month+"-1 "+"00:00:00";
      try {
        months.put("validity_start",String.valueOf(new SimpleDateFormat("yyyy-M-d HH:mm:ss").parse(start).getTime() / 1000));
        months.put("validity_end",String.valueOf(new SimpleDateFormat("yyyy-M-d HH:mm:ss").parse(end).getTime() / 1000));
      } catch (final Exception ignored) {}
      lists.add(months);
    }
    return lists;
  }

  //拿到给定年月得到天数
  public static int getMoneyDay2(int year,int month){
    final Calendar calendar = Calendar.getInstance();//获取当前时间
    calendar.set(Calendar.YEAR,year);
    calendar.set(Calendar.MONTH, month-1);// Calendar月份是以0开始的 所以要-1
    calendar.set(Calendar.DATE, 1);//把日期设置为当月第一天
    calendar.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
    return calendar.get(Calendar.DATE);
  }
}